package org.apache.rocketmq.console.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.rocketmq.console.script.model.RegionInfo;
import org.apache.rocketmq.console.script.service.RegionInfoService;
import org.apache.rocketmq.console.support.JsonResult;
import org.apache.rocketmq.console.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/regionInfo")
public class RegionInfoController {

	@Autowired
	private RegionInfoService regionInfoService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	@ResponseBody
	public boolean save(@RequestBody Map<String, Object> scriptInfoMap) {
		RegionInfo regionInfo = new RegionInfo();

		List<String> deptCodes = new ArrayList<>();
		List<Map<String, Object>> deptCodesAndName = (List<Map<String, Object>>) scriptInfoMap.get("deptCodesAndName");
		for (Map<String, Object> map : deptCodesAndName) {
			String deptCode = (String) map.get("deptCode");
			deptCodes.add(deptCode);
		}
		String deptCodesJson = JsonUtil.obj2String(deptCodes);
		regionInfo.setDeptCodes(deptCodesJson);

		String regionCode = (String) ((Map<String, Object>) scriptInfoMap.get("regionName")).get("regionCode");
		regionInfo.setCode(regionCode);

		String regionName = (String) ((Map<String, Object>) scriptInfoMap.get("regionName")).get("regionName");
		regionInfo.setName(regionName);

		String department = (String) scriptInfoMap.get("department");
		regionInfo.setDepartment(department);

		regionInfoService.save(regionInfo);
		return true;
	}

	// http://localhost:8081/regionInfo/addDept.do?regionCode=BASIC&&deptcode=W01
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addDept.do", method = RequestMethod.GET)
	@ResponseBody
	public Object addDept(@RequestParam("regionCode") String regionCode, @RequestParam("deptcode") String deptcode) {
		RegionInfo findByRegionCode = regionInfoService.findByRegionCode(regionCode);
		if (findByRegionCode == null) {
			return new JsonResult<String>("域名不存在");
		}
		String deptCodes = findByRegionCode.getDeptCodes();
		Set<String> deptCodeList;
		if (deptCodes == null || "".equals(deptCodes)) {
			deptCodeList = new HashSet<String>();
		} else {
			deptCodeList = JSON.parseObject(deptCodes, Set.class);
		}
		deptCodeList.add(deptcode);
		String deptCodeListJson = JSON.toJSONString(deptCodeList);
		findByRegionCode.setDeptCodes(deptCodeListJson);
		regionInfoService.save(findByRegionCode);

		return new JsonResult<RegionInfo>(findByRegionCode);
	}

}
